/*
 * Title: WADS
 * Description: Web-based Auto flow Documents System
 * Copyright: 2007
 * Company: CoE
 * @author: Mr.Nattasan Wang-Aphisit
 * @version 1.1
 */

package wads.applet;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.ObjectInputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JApplet;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import netscape.javascript.JSObject;

public class Sending extends JApplet {
  private static final long serialVersionUID = -4613236224751728566L;
  
  private boolean isStandalone = false;
  //Change serverName IP_Address of Admin's Computer
  //add code to manage ServerName

  private String serverName;

  private String empty = "<-Empty->";
  private Font appletFont = new Font("CordiaUPC", 0, 20);

  private String[] Transaction = new String[20];
  private String[] OwnerNameList = new String[20];
  private int row_of_DocSelect = -1;

  private DefaultMutableTreeNode root = new DefaultMutableTreeNode("Document List");
  private DefaultMutableTreeNode incomming = new DefaultMutableTreeNode("Incoming Document");
  private DefaultMutableTreeNode comeback = new DefaultMutableTreeNode("Comeback Document");
  private JTree tree = new JTree(root);
  private JTextArea jTextArea1 = new JTextArea();

  /**Get a parameter value*/
  public String getParameter(String key, String def) {
    return isStandalone ? System.getProperty(key, def) :
      (getParameter(key) != null ? getParameter(key) : def);
  }

  /**Construct the applet*/
  public Sending() {
  }

  /**Initialize the applet*/
    @Override
  public void init() {

    serverName =  this.getDocumentBase().getHost();

    root.add(incomming);
    root.add(comeback);
    boolean check = true;
    boolean in = false;
    boolean back = false;
    System.out.println("in = "+in+", back = "+back);
    tree.setFont(appletFont);
    tree.setAutoscrolls(true);
    tree.putClientProperty("JTree.lineStyle", "Angled");


    try {
      jbInit();
      //URL url = new URL("http://" + serverName + "/servlet/search");
      URL url = new URL("servlet/search");
      URLConnection conn = url.openConnection();
      ObjectInputStream ois = new ObjectInputStream(conn.getInputStream());

      int count = 0;

      while(check) {
          //Forward Document
          String status = (String)ois.readObject();
          if(status == null){
            check = false;
          }
          String Name = (String)ois.readObject();
          OwnerNameList[count] = Name;
          if(status.compareTo("1") == 0) {
            incomming.add(new DefaultMutableTreeNode(Name));
            in = true;
          }
	  
          if(status.compareTo("0") == 0) {
            comeback.add(new DefaultMutableTreeNode(Name));
            back = true;
          }
	  
          if(incomming.isLeaf() && check == false) {
            incomming.add(new DefaultMutableTreeNode(empty));
	  }
          
          if(comeback.isLeaf() && check == false) {
            comeback.add(new DefaultMutableTreeNode(empty));
	  }
          
          Transaction[count] = (String) ois.readObject();
          count++;
      }
      ois.close();
    }
    catch(Exception e) {
      System.out.println("Exception thrown from Sending.init()");
      e.printStackTrace();
    }

    tree.addMouseListener(new MouseAdapter() {
            @Override
      public void mousePressed(MouseEvent e) {
        JTree t = (JTree)e.getSource();
        int row = t.getRowForLocation(e.getX(), e.getY());
        TreePath path = t.getPathForRow(row);
        TreeNode node = (TreeNode)path.getLastPathComponent();

        if((e.getClickCount() == 2) && node.isLeaf()){
          jTextArea1.setFont(appletFont);
          //char[] ID = new char[6];
          //Token String to get Transaction ID
          //Transaction[row-2].getChars(6, 12, ID, 0);
          if(node.getParent() == incomming)
            row_of_DocSelect = row - 2;
          if(node.getParent() == comeback)
            row_of_DocSelect = row - 3;
          triggerjs();
        }
      }
    });
  }

  /**Component initialization*/
  private void jbInit() throws Exception {
    //this.setSize(new Dimension(323, 300));
    this.getContentPane().add(jTextArea1, BorderLayout.SOUTH);
    this.getContentPane().add(tree, BorderLayout.CENTER);
  }


  /**Get Applet information*/
    @Override
  public String getAppletInfo() {
    return "Applet Information";
  }
  /**Get parameter info*/
    @Override
  public String[][] getParameterInfo() {
    return null;
  }

  public void triggerjs(){
    //Get a handle on the window
    JSObject win = JSObject.getWindow(this);
    //Set the argument object
    String SelectedDocName = Transaction[row_of_DocSelect];
    Object[] args = {"servlet/html?filename=" + SelectedDocName};
    //Call jsfunction() in HTML page
    win.call("jsfunction", args);
  }

  //static initializer for setting look & feel
  static {
    try {
      //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    }
    catch(Exception e) { e.printStackTrace(); }
  }
}